<script lang="ts" setup>
import { ref, onMounted, onBeforeUnmount, provide, computed } from "vue";
import * as _ from "@usemath/utils";

interface SelectProps {
  mode?: "single" | "multiple";
}

const props = withDefaults(defineProps<SelectProps>(), {
  mode: "single",
});
const visible = ref(false);
const viewRef = ref<HTMLElement | null>();
const listRef = ref<HTMLElement | null>();
const options = ref<any>([]);
const viewWidth = ref(0);
const selectedKeys = ref([]);

provide("options", options);
provide("mode", props.mode);
provide("visible", visible);
provide("selectedKeys", selectedKeys);

const listStyles = computed(() => {
  return {
    width: `${viewWidth.value}px`,
  };
});

const clickOutsideHandler = (e: any) => {
  if (!e.target.closest(".umrp-select")) {
    visible.value = false;
  }
};

onMounted(() => {
  document.addEventListener("click", clickOutsideHandler);

  if (viewRef.value) {
    viewWidth.value = viewRef.value.getBoundingClientRect().width;
  }
});

onBeforeUnmount(() => {
  document.removeEventListener("click", clickOutsideHandler);
});
</script>

<template>
  <div class="umrp-select">
    <div class="umrp-select-view" @click="visible = !visible" ref="viewRef">
      <div v-if="selectedKeys.length > 0" class="umrp-select-tag-wrapper">
        <div class="umrp-select-tag" v-for="option in options" v-if="mode === 'multiple'">
          <span>{{ option.label }}</span>
          <umrp-icon name="close" :size="10"></umrp-icon>
        </div>
        <div v-else>
          <umrp-text>{{ _.find(options, { value: selectedKeys[0] })?.label }}</umrp-text>
        </div>
      </div>
      <div v-else class="umrp-select-placeholder">请选择</div>
    </div>
    <div v-show="visible" class="umrp-select-option-wrapper" ref="listRef" :style="listStyles">
      <template v-if="mode === 'multiple'">
        <slot></slot>
      </template>
      <template v-else>
        <slot></slot>
      </template>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.umrp-select {
  position: relative;

  &-tag {
    background-color: #fff;
    font-size: 12px;
    padding: 2px 6px;
    display: flex;
    align-items: center;
    gap: 2px;

    &-wrapper {
      display: flex;
      gap: 4px;
    }
  }

  &-view {
    cursor: pointer;
    box-sizing: border-box;
    font-size: 13.3333px;
    line-height: 32px;
    padding: 0 10px;
    color: var(--text-gray-1);
    border: 1px solid var(--border-color);
    border-radius: 2px;
    background-color: rgb(242, 243, 245);
  }

  &-value {
    box-sizing: border-box;
    font-size: 13.3333px;
    padding: 6px 0;
    text-indent: 10px;
    color: var(--text-gray-1);
    border: 1px solid var(--border-color);
    border-radius: 4px;
  }

  &-option-wrapper {
    cursor: pointer;
    position: absolute;
    z-index: 10;
    top: 36px;
    left: 0;
    border-radius: 2px;
    padding: 0 12px;
    line-height: 40px;
    display: block;
    background-color: #fff;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
  }
}
</style>
